package anthology.daily_question._25_02;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

//https://leetcode.cn/problems/subsets-ii/description/?envType=daily-question&envId=2025-02-05
public class _0090子集2 {
    class Solution {
        List<Integer> t = new ArrayList<Integer>();
        List<List<Integer>> ans = new ArrayList<List<Integer>>();

        public List<List<Integer>> subsetsWithDup(int[] nums) {
            Arrays.sort(nums);
            int n = nums.length;
            for (int mask = 0; mask < (1 << n); ++mask) {
                t.clear();
                boolean flag = true;
                for (int i = 0; i < n; ++i) {
                    if ((mask & (1 << i)) != 0) {
                        if (i > 0 && (mask >> (i - 1) & 1) == 0 && nums[i] == nums[i - 1]) {
                            flag = false;
                            break;
                        }
                        t.add(nums[i]);
                    }
                }
                if (flag) {
                    ans.add(new ArrayList<Integer>(t));
                }
            }
            return ans;
        }
    }
}
